package cn.otrue.patienthealthmanager.activity;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;

/**
 * 修改密码
 * 
 * @author G
 * 
 */
public class PasswordChangeActivity extends BaseActivity {
	private TextView tv_title_title;
	private TextView tv_title_back;
	private EditText et_old_password;
	private EditText et_new_password;
	private EditText et_new_apassword;
	private Button btn_change;
	private String from;
	private String token;// 忘记密码中验证验证码成功后返回的值//用于接口判断修改密码的用户是否可以修改
	private String verification_code;// 返回的验证码
	private String phone;
	private RelativeLayout rl_new_password;
	private Handler changePasswordHandler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			if (state.equals(HttpCons.STATE_SUCCESS)) {
				Toast.makeText(PasswordChangeActivity.this,
						getString(R.string.msg_change_success), 0).show();
				PasswordChangeActivity.this.finish();
			} else {
				Toast.makeText(PasswordChangeActivity.this, msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(PasswordChangeActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_password_change);
		initView();
	}

	public void initView() {
		initTitleBar();
		Intent fromIntent = getIntent();
		from = fromIntent.getStringExtra("from");

		et_old_password = (EditText) findViewById(R.id.et_old_password);
		et_new_password = (EditText) findViewById(R.id.et_new_password);
		et_new_apassword = (EditText) findViewById(R.id.et_new_apassword);
		rl_new_password = (RelativeLayout) findViewById(R.id.rl_new_password);
		btn_change = (Button) findViewById(R.id.btn_change);
		btn_change.setOnClickListener(this);
		if ("passwordback".equals(from)) {
			rl_new_password.setVisibility(View.GONE);
			token = fromIntent.getStringExtra("token");
			verification_code = fromIntent.getStringExtra("verification_code");
			phone = fromIntent.getStringExtra("phone");
		}
	}

	public void initTitleBar() {
		tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(getString(R.string.change_password));

		tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_title_back:
			finish();
			break;
		case R.id.btn_change:
			changePassword();
			break;
		default:
			break;
		}
	}

	private void changePassword() {
		String old_password = et_old_password.getText().toString().trim();
		String new_password = et_new_password.getText().toString().trim();
		String new_apassword = et_new_apassword.getText().toString().trim();
		if (!("passwordback".equals(from)) & TextUtils.isEmpty(old_password)) {
			Toast.makeText(this, getString(R.string.msg_oldpw), 0).show();
		} else if (TextUtils.isEmpty(new_password)) {
			Toast.makeText(this, getString(R.string.msg_newpw), 0).show();
		} else if (TextUtils.isEmpty(new_apassword)) {
			Toast.makeText(this, getString(R.string.msg_againpw), 0).show();
		} else if (!new_password.equals(new_apassword)) {
			Toast.makeText(this, getString(R.string.msg_again_new), 0).show();
		} else {
			if (!("passwordback".equals(from))) {
				if (!(old_password
						.matches("(?!^\\d+$)(?!^[a-zA-Z]+$)[0-9a-zA-Z]{6,10}"))
						|| !(new_apassword
								.matches("?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$"))) {
					Toast.makeText(PasswordChangeActivity.this,
							"密码请输入6-10位字母数字组合", Toast.LENGTH_SHORT).show();
				} else {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("primaryCode", old_password);
					map.put("confimPassword", new_apassword);
					TransferParameter parameter = new TransferParameter();
					parameter.setUsername(MyConst.getACCOUNT());
					parameter.setPassword(new_password);
					parameter.setData(map);
					new HttpServer().post(HttpCons.RETRIEVE_PASSWORD,
							parameter, changePasswordHandler);
				}
			} else {
				// 忘记密码时的修改密码
				if (new_apassword
						.matches("(?!^\\d+$)(?!^[a-zA-Z]+$)[0-9a-zA-Z]{6,10}")) {
					Toast.makeText(PasswordChangeActivity.this,
							"密码请输入6-10位字母数字组合", Toast.LENGTH_SHORT).show();
				} else {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("confimPassword", new_password);
					map.put("code", verification_code);
					map.put("token", token);
					map.put("aliasAccount", "p_" + phone);
					TransferParameter parameter = new TransferParameter();
					parameter.setPassword(new_password);
					parameter.setData(map);
					new HttpServer().post(HttpCons.forgetPassword, parameter,
							changePasswordHandler);
				}
			}
		}

	}
}
